//读取文件内容
String loadFile(String file_name) {
  File dataFile = SPIFFS.open(file_name, "r");
  String  ss;
  if (dataFile)
  {
    Serial.print(file_name);
    Serial.print(" > 大小 > ");
    Serial.print(dataFile.size());
    ss =  dataFile.readString();
    Serial.println(" > 加载完成..");
  }
  else {
    Serial.println("加载");
    Serial.print(file_name);
    Serial.println("未找到");
  }
  dataFile.close();
  return ss;
}


boolean loadConf() {
  String jsonconf = loadFile(TIMER_CONF_FILE_NAME);
  if (jsonconf == "" || jsonconf == "null")
  {
    Serial.println("no conf file");

    return false;
  }
  StaticJsonDocument<256> doc;
  deserializeJson(doc, jsonconf);

  // cycle min
  String cn_str = doc[TIMER_JSON_CONF_KEY_CYCLE_MIN];
  if (cn_str != "")
  {
    Serial.println("TIMER_JSON_CONF_KEY_CYCLE_MIN " + cn_str);
    cn_val = atoi(cn_str.c_str());
  }
  // start min
  String wmn_str = doc[TIMER_JSON_CONF_KEY_WAIT_MIN];
  if (wmn_str != "")
  {
    Serial.println("TIMER_JSON_CONF_KEY_WAIT_MIN " + wmn_str);
    wmn_val = atoi(wmn_str.c_str());
  }
  // run min
  String rmn_str = doc[TIMER_JSON_CONF_KEY_RUN_MIN];
  if (rmn_str != "")
  {
    Serial.println("TIMER_JSON_CONF_KEY_RUN_MIN " + rmn_str);
    rmn_val = atoi(rmn_str.c_str());
  }

  //begin hour
  String bhn_str = doc[TIMER_JSON_CONF_KEY_BEGIN_HOUR];
  if (bhn_str != "")
  {
    Serial.println("TIMER_JSON_CONF_KEY_BEGIN_HOUR " + bhn_str);
    bhn_val = atoi(bhn_str.c_str());
  }
  //end hour
  String ehn_str = doc[TIMER_JSON_CONF_KEY_END_HOUR];
  if (ehn_str != "")
  {
    Serial.println("TIMER_JSON_CONF_KEY_END_HOUR " + ehn_str);
    ehn_val = atoi(ehn_str.c_str());
  }
  doc.clear();
  return true;
}


void  saveJsonConf()
{

  StaticJsonDocument<256> doc;
  doc[TIMER_JSON_CONF_KEY_CYCLE_MIN] = String(cn_val);
  doc[TIMER_JSON_CONF_KEY_WAIT_MIN] =  String(wmn_val);
  doc[TIMER_JSON_CONF_KEY_RUN_MIN] = String(rmn_val);
  doc[TIMER_JSON_CONF_KEY_BEGIN_HOUR] =  String(bhn_val);
  doc[TIMER_JSON_CONF_KEY_END_HOUR] = String(ehn_val);



  String timerconf;
  serializeJson(doc, timerconf);
  Serial.println ("[SAVE SETTING]>>" + timerconf);

  File dataFile = SPIFFS.open(TIMER_CONF_FILE_NAME, "w+");
  dataFile.print(timerconf);
  dataFile.close();
  doc.clear();
}
